package com.ruoyi.erp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.erp.domain.ReceivablePlan;
import com.ruoyi.erp.domain.SettleReceivable;
import com.ruoyi.erp.service.IReceivablePlanService;
import com.ruoyi.erp.service.ISettleReceivableService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ReceivableAccount;
import com.ruoyi.erp.service.IReceivableAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收款记录Controller
 * 
 * @author sauger
 * @date 2022-03-29
 */
@Controller
@RequestMapping("/erp/receivable/detail")
public class ReceivableAccountController extends BaseController
{
    private String prefix = "erp/receivable/detail";

    @Autowired
    private IReceivableAccountService receivableAccountService;
    @Autowired
    private IReceivablePlanService planService;
    @Autowired
    private ISettleReceivableService settleReceivableService;

    @RequiresPermissions("erp:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /**
     * 查询收款记录列表
     */
    @RequiresPermissions("erp:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReceivableAccount receivableAccount)
    {
        startPage();
        List<ReceivableAccount> list = receivableAccountService.selectReceivableAccountList(receivableAccount);
        return getDataTable(list);
    }

    /**
     * 导出收款记录列表
     */
    @RequiresPermissions("erp:detail:export")
    @Log(title = "收款记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReceivableAccount receivableAccount)
    {
        List<ReceivableAccount> list = receivableAccountService.selectReceivableAccountList(receivableAccount);
        ExcelUtil<ReceivableAccount> util = new ExcelUtil<ReceivableAccount>(ReceivableAccount.class);
        return util.exportExcel(list, "收款记录数据");
    }

    /**
     * 新增收款记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/add/other/{id}")
    public String addOther(@PathVariable("id") Integer id, ModelMap mmap)
    {

        ReceivablePlan plan = planService.selectReceivablePlanById(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("plan", plan);
        mmap.put("now", format.format(new Date()));
        mmap.put("userId", getUserId());
        String ret;
        if(plan.getPlanType().equals("staffing"))
        {
            ret = prefix + "/add_staffing";
            List<SettleReceivable> settleReceivables = settleReceivableService.selectSettleReceivableByPlanId(id);
            mmap.put("settleReceivables", settleReceivables);
        }else
        {
            ret = prefix + "/add";
        }

        return ret;
    }

    /**
     * 新增保存收款记录
     */
    @RequiresPermissions("erp:detail:add")
    @Log(title = "收款记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReceivableAccount receivableAccount)
    {
        return toAjax(receivableAccountService.insertReceivableAccount(receivableAccount));
    }

    /**
     * 修改收款记录
     */
    @RequiresPermissions("erp:detail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ReceivableAccount receivableAccount = receivableAccountService.selectReceivableAccountById(id);
        SettleReceivable search = new SettleReceivable();
        search.setReceivableDetailId(id);
        receivableAccount.setSettleReceivable(settleReceivableService.selectSettleReceivableList(search));
        mmap.put("receivableAccount", receivableAccount);
        if (receivableAccount.getPlan().getPlanType().equals("staffing"))
        {
            return prefix + "/edit_staffing";
        }else {
            return prefix + "/edit";
        }

    }

    /**
     * 修改保存收款记录
     */
    @RequiresPermissions("erp:detail:edit")
    @Log(title = "收款记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReceivableAccount receivableAccount)
    {
        return toAjax(receivableAccountService.updateReceivableAccount(receivableAccount));
    }

    /**
     * 删除收款记录
     */
    @RequiresPermissions("erp:detail:remove")
    @Log(title = "收款记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids)
    {
        int id = ids;
        return toAjax(receivableAccountService.deleteReceivableAccountById(id));
    }

    @RequiresPermissions("erp:detail:view")
    @GetMapping("/{id}/detail")
    public String detailByPlanId(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ReceivablePlan plan = planService.selectReceivablePlanById(id);
        mmap.put("plan", plan);
        mmap.put("title",plan.getTitle() + "第" + DictUtils.getDictLabel("account_number", plan.getPlanNumber()) + "笔");
        return prefix + "/plan_detail";
    }

    @RequiresPermissions("erp:receivable:approve")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String ids, Integer status)
    {
        return  toAjax(receivableAccountService.approve(ids, status));
    }

}
